package com.harboursoftware.xstorage.unit.web


import org.junit.After 
import org.junit.Before 
import org.junit.Test 
import org.springframework.mock.web.MockHttpServletRequest 

import com.harboursoftware.xstorage.model.XBucket 
import com.harboursoftware.xstorage.model.XObject 
import com.harboursoftware.xstorage.model.XService 
import com.harboursoftware.xstorage.operations.CopyObject 
import com.harboursoftware.xstorage.operations.DeleteBucket 
import com.harboursoftware.xstorage.operations.DeleteObject 
import com.harboursoftware.xstorage.operations.GetBucket 
import com.harboursoftware.xstorage.operations.GetBucketAcl 
import com.harboursoftware.xstorage.operations.GetObject 
import com.harboursoftware.xstorage.operations.GetObjectAcl 
import com.harboursoftware.xstorage.operations.GetService 
import com.harboursoftware.xstorage.operations.HeadObject 
import com.harboursoftware.xstorage.operations.PostObject 
import com.harboursoftware.xstorage.operations.PutBucket 
import com.harboursoftware.xstorage.operations.PutBucketAcl 
import com.harboursoftware.xstorage.operations.PutObject 
import com.harboursoftware.xstorage.web.StorageServlet;

class StorageServletTest {
	StorageServlet servlet
	def secretAccessKey = 'uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o'
	
	@Before
	void setUp() {
		servlet = new StorageServlet()
	}
	
	@After
	void tearDown() {
	    servlet = null
	}
	
	/**
	 * for more details ,refer to http://docs.amazonwebservices.com/AmazonS3/2006-03-01/dev/index.html?UsingHTTPPOST.html
	 */
	@Test
    void test_create_bucket_and_object_with_path_style_method() {
		MockHttpServletRequest request = new MockHttpServletRequest('get', '/johnsmith/homepage.html')
		request.setServerName 'S.X-storage.com'
		//request.setRequestURI '/johnsmith/homepage.html'
		
		def bucket = servlet.createBucket(request)
		def object = servlet.createObject(request)
		
		assert bucket.name == 'johnsmith'
		assert object.key == '/homepage.html'
	}
	
	/**
	 * for more details ,refer to http://docs.amazonwebservices.com/AmazonS3/2006-03-01/dev/index.html?UsingHTTPPOST.html
	 */
	@Test
	void test_create_bucket_and_object_with_virtual_hosted_style_method() {
		MockHttpServletRequest request = new MockHttpServletRequest('get', '/pages/homepage.html')
		request.setServerName 'Johnsmith.S.X-storage.com'
		
		def bucket = servlet.createBucket(request)
		def object = servlet.createObject(request)
		assert bucket.name == 'johnsmith'
		assert object.key == '/pages/homepage.html'
	}
	
	/**
	 * for more details ,refer to http://docs.amazonwebservices.com/AmazonS3/2006-03-01/dev/index.html?UsingHTTPPOST.html
	 */
	@Test
	void test_create_bucket_and_object_with_CNAME_method() {
		MockHttpServletRequest request = new MockHttpServletRequest('get', '/image/homepage.jpg')
		request.setServerName 'imaGes.johnsmith.net'
		
		def bucket = servlet.createBucket(request)
		def object = servlet.createObject(request)
		
		assert bucket.name == 'images.johnsmith.net'
		assert object.key == '/image/homepage.jpg'
	}
	
    
    @Test
    void test_createOperation_get_service() {
       assert servlet.createOperation('GET', new XService(), [:]) instanceof GetService
    }
	
    @Test
    void test_createOperation_delete_bucket() {
       assert servlet.createOperation('DELETE', new XBucket(), [:]) instanceof DeleteBucket
    }
    
    @Test
    void test_createOperation_get_bucket() {
       assert servlet.createOperation('GET', new XBucket(), [:]) instanceof GetBucket
    }
    
    @Test
    void test_createOperation_get_bucket_acl() {
       assert servlet.createOperation('GET', new XBucket(), [acl : '']) instanceof GetBucketAcl
    }
    
    @Test
    void test_createOperation_put_bucket() {
       assert servlet.createOperation('PUT', new XBucket(), [:]) instanceof PutBucket
    }
    @Test
    void test_createOperation_put_bucket_acl() {
       assert servlet.createOperation('PUT', new XBucket(), [acl : '']) instanceof PutBucketAcl
    }
    
    @Test
    void test_createOperation_delete_object() {
       assert servlet.createOperation('DELETE', new XObject(), [:]) instanceof DeleteObject
    }
    
    @Test
    void test_createOperation_get_object() {
       assert servlet.createOperation('GET', new XObject(), [:]) instanceof GetObject
    }
    
    @Test
    void test_createOperation_get_object_acl() {
       assert servlet.createOperation('GET', new XObject(), [acl : '']) instanceof GetObjectAcl
    }
    
    @Test
    void test_createOperation_head_object() {
       assert servlet.createOperation('HEAD', new XObject(), [:]) instanceof HeadObject
    }
    
    @Test
    void test_createOperation_post_object() {
       assert servlet.createOperation('POST', new XObject(), [:]) instanceof PostObject
    }
    
    @Test
    void test_createOperation_put_object() {
       assert servlet.createOperation('PUT', new XObject(), [:]) instanceof PutObject
    }
    
    @Test
    void test_createOperation_copy_object() {
       assert servlet.createOperation('COPY', new XObject(), [:]) instanceof CopyObject
    }
}
